import 'package:backend.core/core/domain/entities/api/web/yookassa/api_yookassa_one_time_purchased_item_transaction.dart';
import 'package:equatable/equatable.dart';
import 'package:backend.purchases/infrastructure/dtos/api/api_money_amount_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/api_time_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/api_web_one_time_purchased_item_transaction_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/api_web_one_time_purchased_item_transaction_offer_type_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/api_web_transaction_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/yookassa/api_yookassa_transaction_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/yookassa/api_yookassa_transaction_revocation_info_dto.dart';

class ApiYookassaOneTimePurchasedItemTransactionDto extends Equatable
    implements ApiWebOneTimePurchasedItemTransactionDto, ApiYookassaTransactionDto {
  const ApiYookassaOneTimePurchasedItemTransactionDto._internal({
    required this.id,
    required this.amountDto,
    required this.offerTypeDto,
    required this.timeDto,
    required this.revocationInfoDto,
    required this.incomeAmountDto,
  });

  @override
  final String id;

  @override
  final ApiMoneyAmountDto amountDto;

  @override
  final ApiWebOneTimePurchasedItemTransactionOfferTypeDto offerTypeDto;

  @override
  final ApiTimeDto timeDto;

  @override
  final ApiYookassaTransactionRevocationInfoDto? revocationInfoDto;

  @override
  final ApiMoneyAmountDto incomeAmountDto;

  @override
  List<Object?> get props {
    return [
      id,
      amountDto,
      offerTypeDto,
      timeDto,
      revocationInfoDto,
      incomeAmountDto,
    ];
  }

  @override
  String get paymentSystem => kApiWebTransactionYookassaPaymentSystemValue;

  factory ApiYookassaOneTimePurchasedItemTransactionDto.fromEntity(
      ApiYookassaOneTimePurchasedItemTransaction transaction) {
    final amountDto = ApiMoneyAmountDto.fromApiMoneyAmount(transaction.amount);
    final offerTypeDto =
        ApiWebOneTimePurchasedItemTransactionOfferTypeDto.fromEntity(transaction.offerType);
    final transactionTimeDto = ApiTimeDto.fromEntity(transaction.time);
    final incomeAmountDto = ApiMoneyAmountDto.fromApiMoneyAmount(transaction.incomeAmount);

    ApiYookassaTransactionRevocationInfoDto? revocationInfoDto;

    if (transaction.revocationInfo != null) {
      revocationInfoDto =
          ApiYookassaTransactionRevocationInfoDto.fromEntity(transaction.revocationInfo!);
    }

    return ApiYookassaOneTimePurchasedItemTransactionDto._internal(
      id: transaction.id,
      amountDto: amountDto,
      offerTypeDto: offerTypeDto,
      timeDto: transactionTimeDto,
      revocationInfoDto: revocationInfoDto,
      incomeAmountDto: incomeAmountDto,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      kApiWebTransactionIdKey: id,
      kApiWebTransactionAmountKey: amountDto.toJson(),
      kApiWebOneTimePurchasedItemTransactionOfferTypeKey: offerTypeDto.toString(),
      kApiWebTransactionTimeKey: timeDto.toString(),
      kApiWebTransactionPaymentSystemKey: paymentSystem,
      kApiYookassaTransactionRevocationInfoKey: revocationInfoDto?.toJson(),
      kApiYookassaTransactionIncomeAmountKey: incomeAmountDto.toJson(),
    };
  }
}
